package com.molyfun.parents.activity.mainpage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.account.EditProfileActivity;
import com.molyfun.parents.activity.common.attachment.AttachmentViewer;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.rest.bean.GetMyPageResponse;
import com.molyfun.parents.rest.bean.UserResponse;
import com.molyfun.parents.utils.TimeUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyPageFragment extends BaseFragment implements View.OnClickListener{

    public static final String TAG = MyPageFragment.class.getSimpleName();

    @InjectView(R.id.student_name)
    TextView mStudentName;
    @InjectView(R.id.card_id)
    TextView mCardId;
    @InjectView(R.id.sex_img)
    ImageView mSexImg;
    @InjectView(R.id.birthday)
    TextView mBrithday;
    @InjectView(R.id.report_list)
    AttachmentViewer mReportList;
    @InjectView(R.id.desc)
    TextView mDesc;
    @InjectView(R.id.desc_file_list)
    AttachmentViewer mDescFileList;
    @InjectView(R.id.edt_btn)
    TextView mEdtBtn;

    public MyPageFragment() {
    }

    public static MyPageFragment newInstance() {
        MyPageFragment fragment = new MyPageFragment();
//        Bundle bundle = new Bundle();
//        bundle.putInt("category", category);
//        fragment.setArguments(bundle);
        return fragment;
    }

    public void onParentTabClick() {
        loadData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_page, container, false);
        ButterKnife.inject(this, view);
        mEdtBtn.setOnClickListener(this);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
        mEdtBtn.setVisibility(View.GONE);
        List<UserResponse.DepartBean> list = MFPApplication.getInstance().getUser().getDepartlist();
        if (list != null && list.size() > 0){
            for (UserResponse.DepartBean departBean : list) {
                if (departBean.isSelected() && "已入学".equals(departBean.getStatus())){
                    mEdtBtn.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.edt_btn:
                EditProfileActivity.startActivity(getContext());
                break;
        }
    }

    private void loadData(){

        showProgressDialog();
        Call<GetMyPageResponse> call = MFPApplication.getInstance().getRestClient().getStudentInfo();
        call.enqueue(new Callback<GetMyPageResponse>() {
            @Override
            public void onResponse(Call<GetMyPageResponse> call, Response<GetMyPageResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    GetMyPageResponse.MyPageBean myPageBean = response.body().getData();
                    mStudentName.setText(myPageBean.getName());
                    mCardId.setText(myPageBean.getCardid());
                    mBrithday.setText(TimeUtils.getDateByTimeMillis(myPageBean.getBirthdatetime()));
                    mReportList.initData(myPageBean.getDiagnosislist());
                    mDesc.setText(myPageBean.getSituationdescription());
                    mDescFileList.initData(myPageBean.getSituationdesclist());
                    mSexImg.setImageResource(myPageBean.getSex() == 0 ? R.mipmap.ic_sex_male : R.mipmap.ic_sex_female);
                }
            }

            @Override
            public void onFailure(Call<GetMyPageResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });

    }

}
